package com.itheima.ydd.controller;

import com.itheima.ydd.common.ResultData;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @Author IT_daShoouZi
 * @Date 2022 10
 */

@RestController
@RequestMapping("/common")
public class CommonController {

    @Value("${youdianda.basePath}")
    private String basePath;
    @Value("${youdianda.path}")
    private String path;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public ResultData upload(MultipartFile file){
        //file是一个临时文件（也就是图片的临时文件图片.temp），需要转存到指定位置，否则本次请求完后临时文件会删除

        //原始文件名
        String originalFilename = file.getOriginalFilename();//。。。.jpg
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //通过uuid从新给图片新名字+suffix(.jpg)
        String fileName = UUID.randomUUID().toString()+suffix;

        //创建一个目录对象
        File dir=new File(basePath);
        File dirFront=new File(path);

        //判断选择的目录是否存在
        if (!dir.exists()){
            //不存在就创建
            dir.mkdirs();
        }if (!dirFront.exists()){
            //不存在就创建
            dir.mkdirs();
        }

        try {
            //将临时文件转存到指定位置
            file.transferTo(new File(basePath+fileName));
           // file.transferTo(new File(path+fileName));
            FileInputStream fis = new FileInputStream(new File(basePath + fileName));
            //输出流
            FileOutputStream fos = new FileOutputStream(new File(path + fileName));
            //先导入坐标commons-io
            IOUtils.copy(fis,fos);
            //关闭资源
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Map<String,Object> data=new HashMap<>();
        data.put("savePath","/upload/images/20211101/" +fileName);
        data.put("name",fileName);
        data.put("mime","image/png");
        data.put("size",5800);

        return  ResultData.ok("",data);
    }

    @GetMapping("/download")
    public void download(String name, HttpServletResponse response) {
        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fis = new FileInputStream(new File(basePath + name));

            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream sos = response.getOutputStream();


            //先导入坐标commons-io
            IOUtils.copy(fis,sos);


            //关闭资源
            fis.close();
            // sos.close();//有response 创建
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
